package com.cn.boot.student.service.student.impl;

import cn.hutool.core.lang.Assert;
import com.cn.boot.student.entity.WordEntity;
import com.cn.boot.student.entity.WordLikeRelationEntity;
import com.cn.boot.student.mapper.WordLikeMapper;
import com.cn.boot.student.mapper.WordMapper;
import com.cn.boot.student.service.common.impl.RedisServiceImpl;
import com.cn.boot.student.service.student.IWordLikeService;
import com.cn.boot.student.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "word:like")
public class WordLikeServiceImpl implements IWordLikeService {
    @Autowired
    private WordLikeMapper wordLikeMapper;
    @Autowired
    private WordMapper wordMapper;
    @Autowired
    private RedisServiceImpl redisService;
    /**
     * 单词点赞
     *
     * @param wordLikeRelationEntity
     * @return
     */
    @Override
    public CommonResult like(WordLikeRelationEntity wordLikeRelationEntity) {
        //得到前端传来的用户id和单词id，通过id查询关联表中是否存在数据
        Assert.notNull(wordLikeRelationEntity,"数据不能为空");
        List<WordLikeRelationEntity> list = wordLikeMapper.findAll(wordLikeRelationEntity);
        if(list.size() == 0) {
            //当list==0时代表当前用户未点赞该单词，进行点赞更新数据,
            Integer result = wordLikeMapper.add(wordLikeRelationEntity);
            if (result == 0) {
                return CommonResult.failed("点赞失败!");
            }
            //获取单词表中的点赞数
            WordEntity wordEntity = wordMapper.findById(wordLikeRelationEntity.getWordId());
            Integer likeNum = wordEntity.getLikeNum();
            //单词表点赞次数+1
            wordEntity.setLikeNum(likeNum + 1);
            //更新数据库数据
            Integer info = wordMapper.updateLikeNum(wordEntity);
            if (info == 0) {
                return CommonResult.failed("点赞失败！！！");
            }
            return CommonResult.success(info,"点赞成功！！！");
        }
        return CommonResult.failed("不能重复点赞！！！");
    }

    /***
     * 取消点赞
     * @param wordLikeRelationEntity
     * @return
     */
    @Override
    public CommonResult delLike(WordLikeRelationEntity wordLikeRelationEntity) {
        //获取单词表中的点赞数
        WordEntity wordEntity = wordMapper.findById(wordLikeRelationEntity.getWordId());
        Integer likeNum = wordEntity.getLikeNum();
        //单词表点赞次数-1,点赞次数不能小于0
        if(likeNum < 1){
            likeNum = 0;
            wordEntity.setLikeNum(likeNum);
        }
        wordEntity.setLikeNum(likeNum - 1);
        //更新数据库数据
        Integer info = wordMapper.updateLikeNum(wordEntity);
        if (info == 0) {
            return CommonResult.failed("取消点赞失败！！！");
        }
        Integer result = wordLikeMapper.del(wordLikeRelationEntity);
        if(result == 1){
            return CommonResult.success(result,"取消点赞成功！！！");
        }
        return CommonResult.failed("取消点赞失败！！！");
    }

    /**
     * 查询用户是否已经点赞
     *
     * @param wordLikeRelationEntity
     * @return
     */
//    @Override
//    public CommonResult isLike(WordLikeRelationEntity wordLikeRelationEntity) {
//        return null;
//    }
}
